<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../dbcom/PayOrderCom.php");
require_once(dirname(__FILE__) . "/../SDK/phpexcel/PHPExcel.php");
require_once(dirname(__FILE__) . "/../dbcom/CouponCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ClosureCom.php");
require_once(dirname(__FILE__) . "/../dbcom/MerchantCom.php");
require_once(dirname(__FILE__) . "/../dbcom/UserCom.php");

$payordercom = new PayOrderCom();
$phpexcel = new PHPExcel();
$merchantcom = new MerchantCom();
$couponcom = new CouponCom();
$closurecom = new ClosureCom();
$usercom = new UserCom();

$cont = $_REQUEST['cont'];
switch($cont){
    case 1://财务报表
        $time = $_REQUEST['time'];
        $mintime = strtotime($time);
        $maxtime = $mintime + 86400;
        $mintime = $mintime - 1;
        $list = $payordercom->getOrderdata($mintime, $maxtime);
        ob_start();
        $phpexcel->getActiveSheet()->setCellValue('A' . 1, '序号');
        $phpexcel->getActiveSheet()->setCellValue('B' . 1, '订单号');
        $phpexcel->getActiveSheet()->setCellValue('C' . 1, '微信订单号');
        $phpexcel->getActiveSheet()->setCellValue('D' . 1, '价格');
        $phpexcel->getActiveSheet()->setCellValue('E' . 1, '时间');
        //var_dump($list);
        $i = 2;
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $phpexcel->getActiveSheet()->setCellValue('A' . $i, $k);
                $phpexcel->getActiveSheet()->setCellValue('B' . $i, $v['order_id']);
                $phpexcel->getActiveSheet()->setCellValue('C' . $i, $v['out_trade_no']);
                $phpexcel->getActiveSheet()->setCellValue('D' . $i, $v['total_fee']);
                $time = date("Y-m-d h:i:s",$v['create_time']);
                $phpexcel->getActiveSheet()->setCellValue('E' . $i, $time);
                $i++;
            }
        }
        $objWriter = new PHPExcel_Writer_Excel2007($phpexcel);
        $file = date("YmdHis", time()) . "order.xlsx";
        $objWriter->save("../Upload/download/" . $file);
        $fileurl = "http://" . $_SERVER['SERVER_NAME'] . "/Upload/download/" . $file;
        echo json_encode(array("code" => 0, "msg" => "success", "data" => $fileurl));
        ob_end_flush();
        break;
    case 2://优惠券报表
        $time = $_REQUEST['time'];
        $mintime = strtotime($time);
        $maxtime = $mintime + 86400;
        $mintime = $mintime - 1;
        $list = $payordercom->getCoupondata($mintime, $maxtime);
        ob_start();
        $phpexcel->getActiveSheet()->setCellValue('A' . 1, '序号');
        $phpexcel->getActiveSheet()->setCellValue('B' . 1, '优惠券自编号');
        $phpexcel->getActiveSheet()->setCellValue('C' . 1, '类型');
        $phpexcel->getActiveSheet()->setCellValue('D' . 1, '价格');
        $phpexcel->getActiveSheet()->setCellValue('E' . 1, '时间');
        $phpexcel->getActiveSheet()->setCellValue('F' . 1, '用户');
        $phpexcel->getActiveSheet()->setCellValue('G' . 1, '核销商户');
        $i = 2;
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                //var_dump($v);
                $phpexcel->getActiveSheet()->setCellValue('A' . $i, $k);
                $couponinfo = $couponcom->getCoupon($v['coupon_id']);
                $phpexcel->getActiveSheet()->setCellValue('B' . $i, $couponinfo['code']);
                if($couponinfo['type'] == 1){
                    $type = "折扣券";
                }else{
                    $type = "满减券";
                }
                $phpexcel->getActiveSheet()->setCellValue('C' . $i, $type);
                $phpexcel->getActiveSheet()->setCellValue('D' . $i, $couponinfo['total_fee']);
                $time = date("Y-m-d h:i:s",$v['time']);
                $phpexcel->getActiveSheet()->setCellValue('E' . $i, $time);
                $userinfo =  $usercom->getUserInfo($v['user_id']);
                $merchantinfo = $merchantcom->getMerchant($v['merchant_id']);
                $phpexcel->getActiveSheet()->setCellValue('F' . $i, $userinfo['name']);
                $phpexcel->getActiveSheet()->setCellValue('G' . $i, $merchantinfo['name']);
                $i++;
            }
        }
        $objWriter = new PHPExcel_Writer_Excel2007($phpexcel);
        $file = date("YmdHis", time()) . "coupon.xlsx";
        $objWriter->save("../Upload/download/" . $file);
        $fileurl = "http://" . $_SERVER['SERVER_NAME'] . "/Upload/download/" . $file;
        echo json_encode(array("code" => 0, "msg" => "success", "data" => $fileurl));
        ob_end_flush();
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined fail"));
        break;
}
exit();
